package com.cn.onetrip.adapter;

import java.util.Vector;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cn.onetrip.activity.R;
import com.cn.onetrip.objects.VideosMediaObj;
import com.cn.onetrip.untility.ActivityTag;
import com.cn.onetrip.untility.SyncImageLoader;
import com.cn.onetrip.untility.SyncImageLoader.OnImageLoadListener;

public class VideosItemAdapter extends BaseAdapter {

	public Vector<VideosMediaObj> mModels = new Vector<VideosMediaObj>();
	private LayoutInflater mInflater;
	private Context mContext;
	private ListView mListView;
	private Handler mhandler;
	private SyncImageLoader syncImageLoader;

	public VideosItemAdapter(Context context, ListView listView, Handler handler) {
		mInflater = LayoutInflater.from(context);
		syncImageLoader = new SyncImageLoader(context);
		mContext = context;
		mListView = listView;
		mhandler = handler;
	}

	public void addVideos(String imageUrl, String title, Integer fileSize) {
		VideosMediaObj model = new VideosMediaObj();
		model.mediaUrl = imageUrl;
		model.name = title;
		model.size = fileSize;
		mModels.add(model);
	}

	public void clean() {
		mModels.clear();
	}

	public void threadQuit() {
		syncImageLoader.threadQuit();
	}

	@Override
	public int getCount() {
		return mModels.size();
	}

	@Override
	public Object getItem(int position) {
		if (position >= getCount()) {
			return null;
		}
		return mModels.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		VideosMediaObj model = mModels.get(position);
		convertView = mInflater.inflate(R.layout.video_list_items, null);
		convertView.setTag(position);
		TextView titleText = (TextView) convertView
				.findViewById(R.id.VideoItemTitle);
		TextView timeText = (TextView) convertView
				.findViewById(R.id.VideoItemTime);
		final ImageView videoImage = (ImageView) convertView
				.findViewById(R.id.VideoItemImageBg);
		syncImageLoader.loadImage(model.mediaUrl, new OnImageLoadListener() {

			@Override
			public void onImageLoad(Drawable drawable) {
				if (drawable != null) {
					BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
					Bitmap bitmap = bitmapDrawable.getBitmap();
					videoImage.setImageBitmap(ActivityTag.toRoundCorner(bitmap,
							50));
					// videoImage.setBackgroundDrawable(drawable);
				}
			}

			@Override
			public void onImageLoad(Integer t, Drawable drawable) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(Integer t) {
				// TODO Auto-generated method stub

			}
		});
		titleText.setText(model.name);
		timeText.setText(mContext.getResources().getString(R.string.video_size)
				+ formatTime(model.size));
		return convertView;
	}

	private String formatTime(int duration) {
		String timeFormat = String.format("%02d'%02d\"", duration / 60,
				duration % 60);
		return timeFormat;
	}

}
